#include <malloc.h>
#include <strings.h>
#include <string.h>

#include <list.h>

/* #define E() printf("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__) */
#define E() do{}while(0)


que_t * que_create(void)
{
	que_t * q = malloc(sizeof(que_t));
	if(NULL == q) return(NULL);
	bzero(q, sizeof(que_t));
	return(q);
}


int que_destroy(que_t *q)
{
	node_t * n;
	if(NULL == q) return(-1);

	while (q->list)
	{
		n = q->list;
		q->list = q->list->next;
		free(n);		
	}

	free(q);
	return(0);
}


node_t * 	node_create(void * data)
{
	node_t * n = malloc(sizeof(node_t));
	if(NULL == n) return(NULL);
	bzero(n, sizeof(node_t));
	n->data = data;
	return(n);
}


node_t * 	node_extract(que_t * q)
{
	node_t * n;

	if(NULL == q || NULL == q->list) return(NULL);
	E(); 
	n = q->list;
	q->list = n->next;
	q->amount--;
	n->next = NULL;
	E();
	/*	LOG("Extracted from que: %s\n", n->filename ); */

	/* e = ((struct event_node *) n->data)->filename; */
	
	return n;
}


int que_add_node(que_t *q, node_t *n)
{
	if (NULL == q || n == NULL) 
	{
		/* printf("Error! q = %p, node = %p\n", q, n); */
		return(-1);
	}

	n->next = q->list;
	q->list = n;
	q->amount++;
	/*	LOG("Added to que: %s\n", q->list->filename ); */

	return(0);	
}



int que_amount(que_t *q)
{
	if (NULL == q) return(-1);
	return(q->amount);
}


node_t * que_find_node(que_t * q,  char * data, compare_t compare)
{

	node_t * node = q->list;
	if (0 == q->amount || NULL == data)
	{
		/*		LOG("empty que or no data : %p %d \n", data, q->amount); */
		return NULL;
	}

	while (node)
	{
		if (0 == compare(node->data, data)  )
		{
			return node;
		}
		node = node->next;
	}

    return(NULL);
}
